package com.demo.math.equationImpl;

import cn.hutool.core.util.RandomUtil;
import com.demo.math.domain.MathStrategy;

public class FillPlusMin2MathStrategy implements MathStrategy {
    @Override
    public String math(int range) {
        return equation(RandomUtil.randomBoolean() ? 1 : -1, range);
    }

    public String equation(int flag, int limit) {
        int a = RandomUtil.randomInt(1, flag < 0 ? limit * 2 : limit);
        int b = RandomUtil.randomInt(limit);

        if (a == b || a - b < 2 || a + b < 2) {
            return equation(flag, limit);
        }
        String two;
        if (flag > 0) {
            two =  RandomUtil.randomInt(1, 10)>2 ? (a + b) + "" : randomCal(true, a + b);

        } else {
            two = RandomUtil.randomInt(1, 10)>2 ? (a - b) + "" : randomCal(false, a - b);
        }
        String one = randomCal(flag > 0, flag > 0 ? a + b : a - b);

        return RandomUtil.randomInt(0, 10)>3 ? one + "=" + two : two + "=" + one;
    }

    private String randomCal(boolean jia, int result) {
        if (jia) {
            int a = RandomUtil.randomInt(1, result);
            int b = result - a;
            return RandomUtil.randomBoolean() ? "(  )+" + b : a + "+(  )";
        } else {
            int a = RandomUtil.randomInt(result, result * 3);
            int b = a - result;
            return RandomUtil.randomBoolean() ? "(  )" + "-" + b : a + "-" + "(  )";
        }
    }


}
